JAVA programming language by Roy Shayan & Chatterjee Ayan

JAVA programming language by Roy Shayan & Chatterjee Ayan

Author:Roy, Shayan & Chatterjee, Ayan
Language: eng
Format: epub
Published: 2016-03-22T16:00:00+00:00


package mypack;

import java.io.*;

public class IO

{

public int input() throws IOException {

int n;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine());

return n;

}

public void output(String s) {

System.out.print("\n\t"+s); }

}

This program contains a class named as IO, and defines two methods, one for taking integer input, the other for making a string output.

Step 6: Save the file and exit the editor to come out into the command prompt.

Step 7: Change to the bin subdirectory.

cd..

Step 8: Compile the IO.java program while staying within bin subdirectory.

Javac mypack\IO.java

Teacher of Computer Science and Software Technology

1/1A, Ananda Chatterjee Lane, Kolkata 700003. Phone: 033 2554 6084. Email: [email protected] Step 9: Create another new file with .java extension inside the bin subdirectory. Suppose the name of this file is Ayan.java. Import the class from the user defined package. Create an object if that class co that the method of that class can be executed.

import java.io.*;

import mypack.IO; // IMPORTING THE IO CLASS FROM THE USER DEFINED PACKAGE mypack class Ayan {

public static void main(String args[]) throws IOException {

int a, b, c;

IO obj = new IO();

obj.output("Enter first number : "); a = obj.input();

obj.output("Enter second number : "); b = obj.input();

c = a + b; obj.output("The sum is : "+c); }

} The advantage of using the user defined package can be understood from this example. If we are to perform some complex jobs many, many times, it is better to put that job into a user defined package and use that package accordingly whenever we need to do that job again. In any normal Java program, we have to write lengthy statements to declare the object for BufferedReader class, the Sysetm.out.print ( ) method for making output, and the Integer.parseInt(br.readLine ( )) method for taking integer input from keyboard. In the example that we have explained, these jobs have been put inside the methods of the class in the user defined package. For any future programs, we can use the package to skip the burden of writing these statements.

INTERFACE

An interface is similar to a class, but it also has some major differences. Differences between a Class and an Interface

1. A class is declared with the keyword ‘class’, but an interface is declared withthe keyword ‘interface’.

2. A class can have attributes and methods, but an interface generally has methods. Attributes within an interface are not relevant.

3. The methods inside a class are declared and defined, but the methods within an interface are declared only, their bodies are not defined. The bodies of the undefined methods of the interface are defined inside any class that uses the interface.

4. If a new class is created from a class, we have to use the keyword ‘extends’. If a new class is created from an interface, we have to use the keyword ‘implements’.

5. The most important difference is that while creating a new class we can extend from only one super class, but there is no restriction in the number of interfaces that can be implemented while creating a new class.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.